﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Timers;



using BawangMemo.BackendModels;
using System.IO;

namespace BawangMemo.BawangMemoView
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Variables

        enum ViewMode
        {
            None,
            MemoView,
            TestView,
            BankView,
            StatisticView
        }

        MainWindowManager _mainWindowManager;
        CourseWrapper _courseWrapper;


        #endregion

        //MainPart Nothing has changed here.
        #region MainPart

        public MainWindow()
        {
            skin.readbrush(0);
            InitializeComponent();
            this.toolBar1.Background = skin.br;
            _mainWindowManager = new MainWindowManager(this, _courseWrapper);
        }

        public MainWindow(CourseWrapper courseWrapper)
        {
            skin.readbrush(0);
            InitializeComponent();
            this.toolBar1.Background = skin.br;
            this.Title = "课程名称： " + courseWrapper.CourseName + "                        总进度：   " + Math.Round((double)(courseWrapper.ListOldWords().Count * 100) / ((double)courseWrapper.WordList.Count), 2) + "%";

            _courseWrapper = courseWrapper;
            _mainWindowManager = new MainWindowManager(this, _courseWrapper);
        }

        private void ToolBarClick(object sender, RoutedEventArgs e)
        {
            string source = ((Button)sender).Name;

            _mainWindowManager.StartView(source);
        }

        #endregion

        #region Time_Changing
        //bool ac_bool = true;

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (this.ShowInTaskbar == true)
            {
                if (this.WindowState == WindowState.Minimized)
                {
                    MainFrame_MiniMizedToTuoPan();
                    //Timer_Begin();
                }
            }
        }

        //private void Window_Deactivate(object sender, EventArgs e)
        //{
        //    if ((this.ShowInTaskbar == true))
        //        //&& (this.WindowState != WindowState.Minimized))
        //    {
        //        //MainFrame_MiniMizedToTuoPan();
        //        ment_time[0] = BawangMemoView.View.Bawang_Setting._minute * 60;
        //        ac_bool = false;
        //        using (StreamWriter sw = new StreamWriter("a.txt", true))
        //        {
        //            sw.WriteLine(ac_bool.ToString() + " " + ment_time[0]);
        //        }
        //        Timer_Begin();
        //    }
        //}

        //private void Window_Activate(object sender, EventArgs e)
        //{
        //    if (ac_bool == false)
        //    {
        //        ac_bool = true;
        //        using (StreamWriter sw = new StreamWriter("a.txt", true))
        //        {
        //            sw.WriteLine(ac_bool.ToString());
        //        }
        //        Timer_close();
        //    }
        //}

        public void MainFrame_MiniMizedToTuoPan()
        {
            if (this.notifyIcon == null)
            {
                IconShow();   //初始化Icon            
                onscreen_flag = true;
            }

            this.ShowInTaskbar = false;
            onscreen_flag = false;
            this.notifyIcon.Visible = true;
        }

        public void MainFrame_OpenFromTuoPan()
        {
            this.Visibility = Visibility.Visible;

            this.WindowState = WindowState.Normal;

            this.ShowInTaskbar = true;
            onscreen_flag = true;

            this.notifyIcon.Visible = false;
        }
        public void IconShow()
        {
            this.notifyIcon = new System.Windows.Forms.NotifyIcon();
            this.notifyIcon.BalloonTipText = "霸王背单词";
            this.notifyIcon.Text = "霸王背单词";
            this.notifyIcon.Icon = new System.Drawing.Icon("Logo.ico");
            notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;
            this.notifyIcon.ShowBalloonTip(500);
            this.notifyIcon.Visible = false;
        }

        System.Windows.Forms.NotifyIcon notifyIcon;
        //System.Timers.Timer t;
        Boolean onscreen_flag; //表示是否显示出来
        //int[] ment_time = new int[] { 900, 8, 20 };//每过多少秒提醒一次

        //private void time_remind(object source, System.Timers.ElapsedEventArgs e)
        //{
        //    System.Timers.Timer tim = (System.Timers.Timer)source;
        //    //如果已经点开了，那么就不用下一个提醒了
        //    if ((onscreen_flag == false) || (ac_bool == false))
        //    {
        //        MessageBox.Show("别玩了，继续背单词吧！");
        //        //这个字符串就是提醒的语句
        //        if ((tim.Interval < ment_time[ment_time.Length - 1] * 1000))
        //        {
        //            int j = 0;
        //            for (int i = 0; i < ment_time.Length; ++i)
        //                if (ment_time[i] * 1000 == tim.Interval)
        //                {
        //                    j = i; break;
        //                }
        //            //找到下一个提醒时刻点
        //            t = new System.Timers.Timer();
        //            // 1000=1 second
        //            t.Interval = (ment_time[j + 1] - ment_time[j]) * 1000;
        //            t.Enabled = true;
        //            t.AutoReset = false;
        //            t.Elapsed += new System.Timers.ElapsedEventHandler(time_remind);
        //        }
        //    }
        //    tim.Stop();
        //    tim.Dispose();
        //    //把这个timer释放掉
        //}
        //private void Timer_close()
        //{
        //    t.Stop();
        //    t.Dispose();
        //}
        //private void Timer_Begin()
        //{
        //    t = new System.Timers.Timer();
        //    // 1000=1 second
        //    if (ment_time[0] * 1000 < 0) t.Interval = 99999999; else t.Interval = ment_time[0] * 1000;
        //    t.Enabled = true;
        //    t.AutoReset = false; //只提醒一次
        //    t.Elapsed += new System.Timers.ElapsedEventHandler(time_remind);
        //    //            t.Start();
        //}


        private void OnNotifyIconDoubleClick(object sender, EventArgs e)
        {
            MainFrame_OpenFromTuoPan();
            //Timer_close();
        }

        #endregion

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            toolBar1.Background = (Brush)FindResource("SkinColor");
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (App.IsShuttingDown)
            {
                App.Current.Shutdown();
            }
        }
    }
}
